package com.iven.xie.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 基于Feign使用Hystrix
 *
 * @author iven.xie
 */
@RestController
@RequestMapping("/api")
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/hello/{name}")
    @HystrixCommand(fallbackMethod = "getDefaultHello")
    public String index(@PathVariable("name") String name) {
        String url = "http://desktop-0oc8dth:30181/hello?name=" + name;
        return restTemplate.getForObject(url, String.class);
    }

    private String getDefaultHello(String name) {
        System.out.println("熔断，默认回调函数");
        return "hello " + name + ", this message send failed ";
    }
}